﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using XMK.Framework.DataAccess;
using XMK.Tadong.Model;
using XMK.Framework.Model;
using XMK.Utils;


namespace XMK.Tadong.DataSearch.Data
{
    public class SearchKeywordData
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;
        public SearchKeywordData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }

        public List<SearchKeywordInfo> QueryKeyword(string prefixWord, PageInfo pageInfo)
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                string.Format("select keyw_id, keyword, keyw_code, keyw_cnt from tb_search_keyword where keyword like '{0}%' and rownum <=10 order by keyw_cnt desc", prefixWord.Trim()),
                delegate
                {
                    return DbCmdHelper.CreateParameters(dbProvider).ToArray();
                });
            cmd.DbCmdInfo.CommandType = CommandType.Text;

            List<SearchKeywordInfo> keywordList = DbManager.GetInfoList(cmd,
                delegate(out SearchKeywordInfo keywordInfo, DataRow row)
                {
                    keywordInfo = new SearchKeywordInfo
                    {
                        KeywID =1,
                        Keyword = ParseHelper.Parse<string>(row["keyword"]),
                        KeywCode = ParseHelper.Parse<string>(row["keyw_code"]),
                        KeywCount = ParseHelper.Parse<int>(row["keyw_cnt"])
                    };
                });
            return keywordList;
        }

        public void AddKeyword(string keyw)
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                "sp_keyword_add",
                delegate
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "keyw").ToArray();
                });
            cmd.DbCmdInfo.CommandType = CommandType.StoredProcedure;
            cmd.SetParamValues(keyw.Trim());

            DbManager.ExecNonQuery(cmd);
        }
    }
}
